package view.admin.equipManage;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import controller.EquipMsgController;
import dao.EquipTypeDao;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import pojo.equip.EquipMsgEntity;
import pojo.equip.EquipTypeEntity;
import view.Main;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Limohan
 */
public class NewEquipController {

    private Main myApp;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public NewEquipController() {
    }
    @FXML
    private JFXTextField name;

    @FXML
    private JFXComboBox<String> type;

    @FXML
    private JFXTextField mode;

    @FXML
    private JFXTextField detail;

    @FXML
    private JFXCheckBox isFactoryEquip;

    @FXML
    private JFXTextField factory;
    @FXML
    void initialize() throws IOException {
        EquipTypeDao equipTypeDao = new EquipTypeDao();
        for(EquipTypeEntity equipTypeEntity:equipTypeDao.findTypeList()){
            type.getItems().add(equipTypeEntity.getName());
        }
     }

    @FXML
    void returnMsgManage(ActionEvent event) throws Exception {
        myApp.goToEquipMsgManagePage();
    }

    @FXML
    void saveNewEquip(ActionEvent event) throws IOException {
        EquipMsgEntity equipMsgEntity = new EquipMsgEntity();
        EquipMsgController equipMsgController = EquipMsgController.getInstance();
        Date date = new Date();
        equipMsgEntity.setCode("PD"+sdf.format(date));
        equipMsgEntity.setDetail(detail.getText());
        equipMsgEntity.setMode(mode.getText());
        equipMsgEntity.setType(type.getValue());
        equipMsgEntity.setName(name.getText());
        equipMsgEntity.setFactory(factory.getText());
        equipMsgEntity.setEquipState("已关闭");
        //是否为工厂设备
        if(isFactoryEquip.isSelected()){
            equipMsgEntity.setRentState("工厂设备");
            equipMsgEntity.setSource("自有设备");
        }else{
            equipMsgEntity.setRentState("未被租用");
            equipMsgEntity.setSource("租用设备");
        }
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(equipMsgController.addMsg(equipMsgEntity));
        alert.showAndWait();
    }

}
